package cn.dkk.create_design.factory.extend_factory_and_config;

import cn.dkk.create_design.factory.pojo.parent.Coffee;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

public class CoffeeFactory {

    private static HashMap<String, Coffee> coffeeMap=new HashMap<>();

    static {
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            properties.load(resourceAsStream);
            Set<Object> keySet = properties.keySet();
            for (Object o : keySet) {
                String path= (String) properties.get(o);
                Class<?> clazz = Class.forName(path);
                Object coffee = clazz.newInstance();
                coffeeMap.put((String) o, (Coffee) coffee);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Coffee getCoffee(String name){
        return coffeeMap.get(name);
    }

}
